package com.shaoshaohuo.app.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.DetailsEntity;
import com.shaoshaohuo.app.entity.FragmentNewCarEntity;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.ui.DetailsActivity;
import com.shaoshaohuo.app.ui.SureOrderPayInformation;
import com.shaoshaohuo.app.utils.CircleImageView;
import com.shaoshaohuo.app.utils.FiveCornerStar;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.view.NumberView;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 商品詳情頁上半部分
 * Created by Administrator on 2017/6/9.
 */

public class DetailsFragmentTop extends Fragment implements View.OnClickListener {
    private DetailsEntity top;
    private ArrayList<View> views;
    private TextView fragment_detatop_iv_sc;
    private DetailsEntity.DataBean data;
    private NumberView deatils_numberView;

    private Button fragment_detalis_top_btn_gm;

    public static DetailsFragmentTop newInstance(Serializable serializable) {
        Bundle args = new Bundle();
        args.putSerializable("top", serializable);
        DetailsFragmentTop fragment = new DetailsFragmentTop();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_details_top, null);
        return inflate;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle arguments = getArguments();

        top = (DetailsEntity) arguments.getSerializable("top");
        init(view);

    }

    private void init(View view) {
        data = top.getData();
        String name = data.getName();
        TextView fragment_detatop_tv_nmae = (TextView) view.findViewById(R.id.fragment_detatop_tv_nmae);
        fragment_detatop_tv_nmae.setText(name);
        TextView fragment_detatop_tv_guige = (TextView) view.findViewById(R.id.fragment_detatop_tv_guige);
        String mbn_details = data.getMbn_details();
        fragment_detatop_tv_guige.setText(mbn_details + "斤/件");
        TextView fragment_detatop_tv_danjian = (TextView) view.findViewById(R.id.fragment_detatop_tv_danjian);
        String details = data.getDetails();
        fragment_detatop_tv_danjian.setText(details + ")");
        TextView fragment_detatop_tv_detalis = (TextView) view.findViewById(R.id.fragment_detatop_tv_detalis);
        //taste_details
        String taste_details = data.getTaste_details();
        fragment_detatop_tv_detalis.setText(taste_details);
        TextView fragment_detatop_tv_money = (TextView) view.findViewById(R.id.fragment_detatop_tv_money);
        String money = data.getMoney();
        fragment_detatop_tv_money.setText("¥" + money);
        TextView fragment_detatop_tv_shopmoney = (TextView) view.findViewById(R.id.fragment_detatop_tv_shopmoney);
        String shop_money = data.getShop_money();
        fragment_detatop_tv_shopmoney.setText(shop_money);
        deatils_numberView = (NumberView) view.findViewById(R.id.deatils_numberView);
        int carNum = data.getCarNum();
        if (carNum <= 0) {
            deatils_numberView.setVisibility(View.GONE);
        } else {
            deatils_numberView.setVisibility(View.VISIBLE);
        }
        deatils_numberView.setNum(carNum + "");
        Button fragment_detalis_top_btn_add = (Button) view.findViewById(R.id.fragment_detalis_top_btn_add);
        fragment_detalis_top_btn_add.setOnClickListener(this);
        fragment_detalis_top_btn_gm = (Button) view.findViewById(R.id.fragment_detalis_top_btn_gm);
        fragment_detalis_top_btn_gm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentNewCarEntity fragmentNewCarEntity = new FragmentNewCarEntity();
                DetailsEntity.DataBean data = top.getData();
                fragmentNewCarEntity.setData(new ArrayList<FragmentNewCarEntity.DataBean>());
                List<FragmentNewCarEntity.DataBean> data1 = fragmentNewCarEntity.getData();
                FragmentNewCarEntity.DataBean dataBean = new FragmentNewCarEntity.DataBean();
                dataBean.setNum(data.getCarNum() + "");
                dataBean.setCatid(data.getCatid());
                dataBean.setDetails(data.getDetails());
                dataBean.setImg(data.getImg());
                dataBean.setNames(data.getName());
                dataBean.setMbn_details(Integer.valueOf(data.getMbn_details()));
                dataBean.setPrice(data.getMoney());
                dataBean.setShop_price(data.getShop_money());
                data1.add(dataBean);
                Intent intent = new Intent();
                intent.putExtra("fragmentNewCarEntity", fragmentNewCarEntity);
                intent.putExtra("gotype", "2");//普通商品到订单
                intent.putExtra("sorttype", "1");//普通商品
                intent.setClass(getContext(), SureOrderPayInformation.class);
                startActivity(intent);
            }
        });

        ViewPager viewPager = (ViewPager) view.findViewById(R.id.fragment_detatop_vp);
        viewPager.getLayoutParams().height = getResources().getDisplayMetrics().heightPixels * 2 / 3;
        List<DetailsEntity.DataBean.ImgMapBean> img_map = data.getImg_map();
        views = new ArrayList<>();
        for (int i = 0; i < img_map.size(); i++) {
            ImageView imageView = new ImageView(getContext());
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            Picasso.with(getContext()).load(img_map.get(i).getImg()).placeholder(R.mipmap.logo).into(imageView);
            views.add(imageView);
        }
        viewPager.setAdapter(new MyvpAdapter());
        RelativeLayout fragment_detatop_lin_gw = (RelativeLayout) view.findViewById(R.id.fragment_detatop_lin_gw);
        fragment_detatop_lin_gw.setOnClickListener(this);

        RelativeLayout fragment_detatop_lin_sc = (RelativeLayout) view.findViewById(R.id.fragment_detatop_lin_sc);
        fragment_detatop_iv_sc = (TextView) view.findViewById(R.id.fragment_detatop_iv_sc);
        fragment_detatop_lin_sc.getChildAt(0);
        String catid = data.getCatid();
        /**
         * 评论相关
         */
        List<DetailsEntity.DataBean.RemarkBean> a = data.getRemark();
        TextView pj_user_value = (TextView) view.findViewById(R.id.pj_user_value);
        if (a.size() > 0) {
            FiveCornerStar pj_user_xx = (FiveCornerStar) view.findViewById(R.id.pj_user_xx);
            pj_user_xx.setIsdown(false);
            pj_user_xx.setNum(5);
            pj_user_xx.setdownnum(Float.parseFloat(data.getRemark().get(0).getRemarkLevel()));
            DetailsEntity.DataBean.RemarkBean remark = a.get(0);
            TextView pj_user_phonenum = (TextView) view.findViewById(R.id.pj_user_phonenum);

            String userPhone = remark.getUserPhone();
            String substring = userPhone.substring(3, 7);
            String replace = userPhone.replace(substring, "****");
            pj_user_phonenum.setText(replace);

            TextView bcd = (TextView) view.findViewById(R.id.bcd);
            String remarkPen = remark.getRemarkPen();
            bcd.setText(remarkPen);
            TextView pj_user_num = (TextView) view.findViewById(R.id.pj_user_num);
            int remarkNum = remark.getRemarkNum();
            pj_user_num.setText(remarkNum + "");
            CircleImageView pj_user_photo = (CircleImageView) view.findViewById(R.id.pj_user_photo);
            Picasso.with(getContext()).load(remark.getUserImg()).placeholder(R.mipmap.logo).into(pj_user_photo);

            String remarkValue = remark.getRemarkValue();
            pj_user_value.setText(remarkValue);
        } else {
            view.findViewById(R.id.pj_1).setVisibility(View.GONE);
            view.findViewById(R.id.pj_2).setVisibility(View.GONE);
            pj_user_value.setText("暂无评论");
        }
        view.findViewById(R.id.pj_user_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailsActivity activity = (DetailsActivity) getActivity();
                activity.setvpseleter(1);
                activity.setAc_detalis_vp(1);

            }
        });
        /*
        ===================================================华丽丽的分割线=======================================================
         */
        //collStatus 1加了0沒有
        if (data.getCollStatus() == 0) {
            //
            fragment_detatop_iv_sc.setSelected(false);
        } else {
            fragment_detatop_iv_sc.setSelected(true);
        }
        fragment_detatop_lin_sc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                View childAt = ((RelativeLayout) v).getChildAt(0);
                if (childAt.isSelected()) {
                    ShopHttpConfig.Collectiondelte(getContext(), data.getCatid(), new Okhttputils.OkutListener() {
                        @Override
                        public void error(Call call, IOException e) {

                        }

                        @Override
                        public void error(Call call, int code) {

                        }

                        @Override
                        public void suseff(Call call, Response response, Object o) {
                            v.post(new Runnable() {
                                @Override
                                public void run() {
                                    View childAt = ((RelativeLayout) v).getChildAt(0);
                                    fragment_detatop_iv_sc.setSelected(false);
                                }
                            });
                        }
                    });
                } else {
                    ShopHttpConfig.Collectionadd(getContext(), data.getCatid(), new Okhttputils.OkutListener() {
                        @Override
                        public void error(Call call, IOException e) {

                        }

                        @Override
                        public void error(Call call, int code) {

                        }

                        @Override
                        public void suseff(Call call, Response response, Object o) {
                            v.post(new Runnable() {
                                @Override
                                public void run() {
                                    Log.e("TAG", "run: =                  ==========");
                                    View childAt = ((RelativeLayout) v).getChildAt(0);
                                    fragment_detatop_iv_sc.setSelected(true);
                                }
                            });

                        }
                    });
                }


            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fragment_detalis_top_btn_add:
            case R.id.fragment_detatop_lin_gw:
                //添加购物车
                ShopHttpConfig.addshop(getContext(), data.getCatid(), new Okhttputils.OkutListener() {
                    @Override
                    public void error(Call call, IOException e) {

                    }

                    @Override
                    public void error(Call call, int code) {

                    }

                    @Override
                    public void suseff(Call call, Response response, Object o) {
                        deatils_numberView.post(new Runnable() {
                            @Override
                            public void run() {
                                deatils_numberView.setVisibility(View.VISIBLE);
                                deatils_numberView.setNum((deatils_numberView.getnum() + 1) + "");
                                top.getData().setCarNum(deatils_numberView.getnum());
                            }
                        });
                    }
                });

                break;
        }
    }

    class MyvpAdapter extends PagerAdapter {
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = views.get(position);
            container.addView(view);
            return view;

        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);
            container.removeView(views.get(position));
        }
    }
}
